<?php
namespace app\common\logic;

use think\facade\Db;
use pay\wechat\WxPay;
use pay\ali\AliPay;
/**
 * 支付类
 */
class Bonus
{

    //推荐奖金 拿直推一代报单区金额的百分比，秒结，秒发，到收益钱包
    public function intro_bonus($order){
        $username = $order['username'];
        $order_pv = $order['total_shop_price'];
        $user_info = Db::name('users')->where('username',$username)->find();
        if(empty($user_info['tjr_id']) || empty($user_info['tjr_str'])){
            return true;
        }
        $tjr_arr = Db::name('users')->where('user_id','in',$user_info['tjr_str'])->order('depth desc')->select()->toArray();
        foreach($tjr_arr as $tjr){
            $intro = $user_info['tjr_name'];
            $bonus_yeji = $order_pv;

            //奖金比例
            $bonus_bili = tpCache('bonus.intro_bonus_'.$tjr['level'].'_1');
            $memo = '推荐会员'.$username.'购物奖励';
            $bonus_type = 'intro_bonus';

            if(empty($bonus_bili)){
                $bonus_bili = 0;
            }
            //$bonus_bili = tpCache('basic.share_cha_bonus_'.$intro_info['level']);
            $bonus_money = bcmul($bonus_yeji,$bonus_bili/100,2);
            //dump($intro_pv.'=>'.$order_pv.'=>'.$bonus_yeji.'=>'.$bonus_bili);
            if($bonus_money>0){
                $res = logic('Wallet')->bonusDetail($tjr,$bonus_type,100,$bonus_money,$memo,'orders',$order['order_id'],0,
                    $user_info['user_id'],$user_info['username'],1);
            }

        }

        return $res;
    }

    //发放奖金
    public function sendBonus($id=0){
        $cuttime = getCurTime();
        $cuttime = strtotime(date('Y-m-d 00:00:00',$cuttime));
        $prize_arr = config('prize');
        $money_types = config('prizetype');
        //先发放分享奖
        $map = [];
        $map['status'] = 0;
        $map['is_del'] = 0;
        if($id){
            $map['id'] = $id;
        }else{
            $map['ff_time'] = ['elt',$cuttime];
        }
        $lists = Db::name('bonus_detail')
            ->where($map)
            ->field('id,bonus bonus_total,user_id,username,bonus_type')
            ->select();
        $wallets = logic('Wallet')->getName();
        $wallets_bili = [];
        foreach($wallets as $wallet_key=>$wallet_name){
            $send_bili = tpCache('bonus.bonus_send_'.$wallet_key);
            $wallets_bili[$wallet_key] = $send_bili?:0;
        }
        foreach($lists as $list){
            $bonus_total = $list['bonus_total'];
            $guanli = 0;

            $bonus_total = bcsub($bonus_total,$guanli,2);
            $desc = '奖金发放'.date('Y-m-d',$list['riqi']);
            $money_type = $money_types[$list['bonus_type']]??'0';

            $data_u = [
                'status'=>1,
                'send_time'=>$cuttime,
            ];
            foreach($wallets_bili as $wallet_key=>$send_bili){
                $send_bonus = bcmul($bonus_total,$send_bili/100,2);
                $data_u[$wallet_key] = $send_bonus;
                $data_u[$wallet_key.'_bili'] = $send_bili;
                if($send_bonus>0){
                    $res = qianbao($list['user_id'],$send_bonus,$wallet_key,$prize_arr[$list['bonus_type']],$desc,0,$money_type);
                    if(!$res['status']){
                        return $res;
                    }
                }
            }
            //累加会员奖金
            $map['user_id'] = $list['user_id'];
            $map['id'] = $list['id'];
            Db::name('bonus_detail')->where($map)->update($data_u);
        }

        $result = [
            'status' => true,
            'msg' => '发放完成',
        ];
        return $result;

    }
}